package Akanksha;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Singleton Link:
 * http://www.javaworld.com/javaworld/jw-04-2003/jw-0425-designpatterns.html
 * @author Stephen
 */
public class SipLogger {
    
    private static SipLogger instance = null;
    
    ///End of Line characters are OS dependent
    private static String eol = System.getProperty("line.separator");
    
    protected SipLogger() {
        // Exists only to defeat instantiation.
        String msg;
        msg = "INFO " + Clock.GetCurrentDateTimeString() + " " + "Starting up" + eol;
        logMessage( msg );
    }
    
    // Obtain a logger.
    ///Should be called on startup.
    public static SipLogger Instance() {
        if(instance == null) {
            instance = new SipLogger();
        }
        return instance;
    }
    
    ///Use to print useful information to the user
    public void info(String message) {
        //record the info message
        String msg;
        msg = "INFO " + Clock.GetCurrentDateTimeString() + " " + message;
        logMessage( msg );
    }
    
    ///Use to log debug messages to both log file and output console/window
    public void debug(String message) {
        //record the debug message to log file and to output console/window
        String msg;
        msg = "DEBUG " + Clock.GetCurrentDateTimeString() + " " + message;
        logMessage( msg );
        System.out.println( msg );
    }
    
    ///Use when you acknowledge what is happening but don't approve
    public void warn(String message) {
        //record the warn message
        String msg;
        msg = "WARN " + Clock.GetCurrentDateTimeString() + " " + message;
        logMessage( msg );
    }
    
    ///Use when there is an error
    public void error(String message) {
        //Prints errors to log file
        String msg;
        msg = "ERROR " + Clock.GetCurrentDateTimeString() + " " + message;
        logMessage(msg);
    }
    
    ///We've got a problem, print the reason and then the program should die
    public void fatal(String message) {
        //Prints fatal message to log file
        String msg;
        msg = "FATAL " + Clock.GetCurrentDateTimeString() + " " + message;
        logMessage( msg );
    }
    
    ///Use to print a log message to the log file
    private void logMessage(String message) {
        //record the message
        try {
            PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("SipLog.txt", true)));
            out.println( message );
            out.close();
        } catch (IOException e) {
            System.out.println("ERROR:  Exception:  " + e.toString());
        }
    }
    
    ///Used for testing this class
//    public static void main (String args[]) {
//         SipLogger.Instance().info("Check this out");
//         SipLogger.Instance().error("Found an error");
//    }
    
}
